//Given two stings ransomNote and magazine, return true if ransomNote can be con
//structed from magazine and false otherwise. 
//
// Each letter in magazine can only be used once in ransomNote. 
//
// 
// Example 1: 
// Input: ransomNote = "a", magazine = "b"
//Output: false
// Example 2: 
// Input: ransomNote = "aa", magazine = "ab"
//Output: false
// Example 3: 
// Input: ransomNote = "aa", magazine = "aab"
//Output: true
// 
// 
// Constraints: 
//
// 
// 1 <= ransomNote.length, magazine.length <= 105 
// ransomNote and magazine consist of lowercase English letters. 
// 
// Related Topics 字符串 
// 👍 145 👎 0


package leetcode.editor.cn;
//Java：Ransom Note
 class P383RansomNote{
    public static void main(String[] args) {
        Solution solution = new P383RansomNote().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        // 统计 ransom 字符
        int[] chars = new int[26];
        int ransomLen = ransomNote.length();
        for (int i = 0; i < ransomLen; i++) {
            chars[ransomNote.charAt(i) - 97]++;
        }
        // 遍历 magazine
        for (int i = 0, len = magazine.length(); i < len && ransomLen > 0; i++) {
            if (chars[magazine.charAt(i) - 97] != 0) {
                chars[magazine.charAt(i) - 97]--;
                ransomLen--;
            }
        }
        return ransomLen == 0;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}